<?php
// +----------------------------------------------------------------------
// | YBlog
// +----------------------------------------------------------------------
// | Copyright (c) 2008 http://www.yhustc.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yhustc <yhustc@gmail.com>
// +----------------------------------------------------------------------
// $Id$

/**
 +------------------------------------------------------------------------------
 * YBlog附件管理模块
 +------------------------------------------------------------------------------
 * @author    yhustc <yhustc@gmail.com>
 * @version   $Id$
 +------------------------------------------------------------------------------
 */


class AttachAction extends BaseAction
{
	/**
     +----------------------------------------------------------
     * 附件上传函数
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     */
	function upload()
	{
		if(isset($_POST["submit"]) && $_POST["upload"] == "1")
		{
			header("Content-type: text/html; charset=utf-8");
			
			$user_config = require "user_config.php";
			if(!is_writeable($user_config['UPLOAD_PATH']))
			{
                echo "<div style='margin: 10px'><p style='color: red'>上传目录".$user_config['UPLOAD_PATH']."不可写</p><a href='javascript:history.go(-1)'>返回</a></div>";
	            exit(-1);
            }
            
			import("@.ORG.UploadFile");
	        $upload = new UploadFile();
	        //设置上传文件大小
	        $upload->maxSize  = $user_config['ATTACHMENT_SIZE'];
	        //设置上传文件类型
	        $upload->allowExts  = explode(',',$user_config['ATTACHMENT_EXT']);
	        //设置附件上传目录
	        //上传文件按月存到不同文件夹,所以先检查文件夹,没有的话就新建
	        $uploadDir = $user_config['UPLOAD_PATH']."/".date("Y-m")."/";
	        if(!is_dir($uploadDir))
			{
				mkdir($uploadDir); //默认的 mode 是 0777
	        }	        
	        $upload->savePath = $uploadDir;
	        
	        //执行上传操作
	        if(!$upload->upload()) {
	            //捕获上传异常
	            echo "<div style='margin: 10px'><p style='color: red'>".$upload->getErrorMsg()."</p><a href='javascript:history.go(-1)'>返回</a></div>";
	            exit(-1);
	        }else{
	        	echo '<script language="javascript">';
	        	$uploadFileInfo = $upload->getUploadFileInfo();
				$attachDao = D("Attach");
				//因为需要取每一个附件所对应的id,所以只能是一条条的添加记录
				foreach($uploadFileInfo as $fileInfo)
				{
					$attachDao->create($fileInfo);
					$id = $attachDao->add();
					if($id) //添加不成功的记录我们就不管了
					{
						$attachJS = $attachDao->parseAttach($this->options,array_merge(array('id'=>$id),$fileInfo));
						echo "parent.".$attachJS;
					}
				}
				echo "document.write('<div style=\'margin: 10px\'>上传附件成功</div>');";
				echo "parent.hideUploadWindow();";
				echo '</script>';				
			}
		}
		else
			$this->display();
	}
	
	/**
     +----------------------------------------------------------
     * 删除附件函数
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     */
	function delAttach()
	{
		$attachDao = D("Attach");
		$attach = $attachDao->getById($_POST["id"]);
		if(count($attach) < 1)
		{
			$this->error("没有与该附件相对应的记录");
		}
		$rs = $attachDao->deleteById($_POST["id"]);
		if(!$rs)
			$this->error("无法删除附件所对应的记录");
		$result = unlink($attach["savepath"].$attach["savename"]);
		if(!$result)
			$this->error("无法删除附件所对应的文件");
			
		$this->success();
	}
}
?>